package com.jit.monitor.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.util.PerfectClickListener;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.CheckDataManager;
import com.jit.monitor.model.response.CtFrameResponse;
import com.jit.monitor.presenter.CtFrameAtPresenter;
import com.jit.monitor.ui.adapter.CtDetailAdapter;
import com.jit.monitor.view.CtFrameAtView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author crazyZhangxl on 2018-12-21 20:21:14.
 * Describe: 当前详细信息-------
 */

public class CurrentDetailActivity extends BaseActivity<CtFrameAtView, CtFrameAtPresenter> implements CtFrameAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView mIvToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View mVToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView mTvToolbarTitle;
    @BindView(R.id.llToolbarTitle)
    LinearLayout mLlToolbarTitle;
    @BindView(R.id.ibAddMenu)
    ImageButton mIbAddMenu;
    @BindView(R.id.etSearchContent)
    EditText mEtSearchContent;
    @BindView(R.id.llToolbarSearch)
    LinearLayout mLlToolbarSearch;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private String frameID, obj;

    private CtDetailAdapter mCtDetailAdapter;
    private List<CtFrameResponse> mCtFrameResponseList = new ArrayList<>();

    @Override
    protected void init() {
        if (getIntent() != null) {
            frameID = getIntent().getStringExtra(AppConstant.FRAME_ID);
            obj = getIntent().getStringExtra(AppConstant.OBJ);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_current_detail;
    }

    public static void showCurrentDetailInfo(Activity activity, String frameID, String obj) {
        Intent intent = new Intent(activity, CurrentDetailActivity.class);
        intent.putExtra(AppConstant.FRAME_ID, frameID);
        intent.putExtra(AppConstant.OBJ, obj);
        activity.startActivity(intent);
    }

    @Override
    protected CtFrameAtPresenter createPresenter() {
        return new CtFrameAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTvToolbarTitle.setText("详细帧数据");
        initAdapter();

    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mCtDetailAdapter = new CtDetailAdapter(R.layout.item_current,mCtFrameResponseList);
        mRecyclerView.setAdapter(mCtDetailAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.queryCtFrameList(frameID, obj);
    }

    @Override
    protected void initListener() {
        mIvToolbarNavigation.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });

        mCtDetailAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvNumber){
                    String id = mCtFrameResponseList.get(position).getId();
                    showMMDialog(id);
                }
            }
        });
    }

    private void showMMDialog(String id) {
        showMaterialDialog("物品编号详情", id, "", "取消", null, new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void queryCtFrameSuccess(List<CtFrameResponse> frameResponseList) {
        mCtFrameResponseList.clear();
        mCtFrameResponseList.addAll(frameResponseList);
        if (mCtFrameResponseList.size() > 0){
            refreshTitle(CheckDataManager.getCheckChineseName(mCtFrameResponseList.get(0).getObjType()));
        }
        mCtDetailAdapter.notifyDataSetChanged();
    }

    private void refreshTitle(String checkChineseName) {
        mTvToolbarTitle.setText("详细帧——"+checkChineseName);
    }

    @Override
    public void queryFrameFailure(String error) {
        UIUtils.showToast(error);
    }

}
